# 代码模拟登录
# 1.找到登录网址  get'https://www.yaozh.com/login'
#               post'https://www.yaozh.com/login'
# 2.找到登录参数 username pwd formhash backurl

# 查找 过程
# 1. form表单 的标签 action如果有值 它才是登录网址
#      form表单的子标签 就是参数 input (name="值" ) 传递给服务器的key

# 2.form表单是空的  只能抓包

import requests

# session功能 就有保存cookie
session = requests.session()
session.headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
}
# 1.实现代码登录, 如果登录成功 保存cookie POST
login_url = "https://www.yaozh.com/login"

formdata = {
    "username": "xiaomaoera12",
    "pwd": "lina081012",
    "formhash": "1A412CC72E",
    "backurl": "%2F%2Fwww.yaozh.com"
}
# 发送登录请求
session.post(login_url, data=formdata)

# 2.带着保存好的cookie  请求目标数据
final_url = "https://db.yaozh.com/user"

# 发送目标数据
response = session.get(final_url)

data = response.content.decode()
with open('1login.html','w') as f:
    f.write(data)
